package com.kinghood.productcenter.service.product.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kinghood.infrastructure.util.UserUtil;
import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.productcenter.common.model.dao.product.UserSearchSettingDO;
import com.kinghood.productcenter.common.model.dto.product.GetUserSearchSettingReqDTO;
import com.kinghood.productcenter.common.model.vo.product.GetUserSearchSettingVO;
import com.kinghood.productcenter.common.model.vo.product.SearchSettingVO;
import com.kinghood.productcenter.common.model.dto.product.UserSearchSettingReqDTO;
import com.kinghood.productcenter.dao.product.UserSearchSettingMapper;
import com.kinghood.productcenter.service.product.IUserSearchSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KingHood
 * @since 2024-01-15
 */
@Service
public class UserSearchSettingServiceImpl extends ServiceImpl<UserSearchSettingMapper, UserSearchSettingDO> implements IUserSearchSettingService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean saveUserSearchSetting(UserSearchSettingReqDTO in) {

        // 删除历史记录
        new LambdaUpdateChainWrapper<>(this.getBaseMapper())
                .eq(UserSearchSettingDO::getUserId, UserUtil.getUserId())
                .eq(UserSearchSettingDO::getBusinessType, in.getBusinessType())
                .eq(UserSearchSettingDO::getBusinessCode, in.getBusinessCode())
                .remove();

        // 当数据是空的时候，需要记录
        if(CollectionUtils.isEmpty(in.getCodeList())){
            UserSearchSettingDO one = new UserSearchSettingDO();
            one.setId(IdGenUtil.getId());
            one.setUserId(UserUtil.getUserId());
            one.setBusinessType(in.getBusinessType());
            one.setBusinessCode(in.getBusinessCode());
            one.setCode("null");
            one.setCreateTime(new Date());
            return this.save(one);
        }

        // 新增
        ArrayList<UserSearchSettingDO> list = new ArrayList<>();
        for (SearchSettingVO searchSettingVO : in.getCodeList()) {
            UserSearchSettingDO one = new UserSearchSettingDO();
            one.setId(IdGenUtil.getId());
            one.setUserId(UserUtil.getUserId());
            one.setBusinessType(in.getBusinessType());
            one.setBusinessCode(in.getBusinessCode());
            one.setCode(searchSettingVO.getCode());
            one.setCodeType(searchSettingVO.getCodeType());
            one.setCreateTime(new Date());
            list.add(one);
        }

        return this.saveBatch(list);
    }

    @Override
    public GetUserSearchSettingVO getUserSearchSetting(GetUserSearchSettingReqDTO in) {

        List<UserSearchSettingDO> list = new LambdaQueryChainWrapper<>(this.getBaseMapper())
                .eq(UserSearchSettingDO::getUserId, UserUtil.getUserId())
                .eq(UserSearchSettingDO::getBusinessCode, in.getBusinessCode())
                .eq(Objects.nonNull(in.getBusinessType()),UserSearchSettingDO::getBusinessType, in.getBusinessType())
                .list();
        GetUserSearchSettingVO res = new GetUserSearchSettingVO();
        if(CollectionUtils.isEmpty(list)){
            return res;
        }
        List<SearchSettingVO> searchList = list.stream()
                .filter(x -> x.getBusinessType() == UserSearchSettingDO.BUSINESS_TYPE_SEARCH)
                .map(x->new SearchSettingVO(x.getCode(),x.getCodeType()))
                .collect(Collectors.toList());
        res.setSearchList(searchList);

        List<SearchSettingVO> showList = list.stream()
                .filter(x -> x.getBusinessType() == UserSearchSettingDO.BUSINESS_TYPE_SHOW)
                .map(x->new SearchSettingVO(x.getCode(),x.getCodeType()))
                .collect(Collectors.toList());
        res.setShowList(showList);

        res.handleEmpty();
        return res;
    }
}
